//
//  ForgetViewController.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/26.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "ForgetViewController.h"
#import "MinButtonViewBtn.h"
#import "JKCountDownButton.h"
#import "ResetPassViewController.h"
#import "GetPhoneModel.h"

@interface ForgetViewController ()<UITextFieldDelegate>{
    NSString *yanZhengMaString;
    NSString *acountString;
    GetPhoneModel *phoneModel;
}
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UIView *lineCountView;
@property (nonatomic, strong) UIView *linePassView;
@property (nonatomic, strong) UIImageView *acountImageView;
@property (nonatomic, strong) UIImageView *passwordImageView;
//@property (nonatomic, strong) UIButton *acountMoreButton;
@property (nonatomic, strong) JKCountDownButton *huoQuYanZhengMaButton;
@property (nonatomic, strong) UIView *topLineView;
@property (nonatomic, strong) UIView *bottomLineVIew;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) ResetPassViewController *resetPassVC;

@property (nonatomic, strong) UITextField *acountTF;
@property (nonatomic, strong) UITextField *passTF;//验证码
@end

@implementation ForgetViewController

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (UITextField *)acountTF{
    if (!_acountTF) {
        UITextField *tf = [[UITextField alloc]init];
        tf.placeholder = @"请输入账号";
        tf.textColor = TITLECOLOR;
        tf.delegate = self;
        NSString *phone = [USERDEFAL objectForKey:AcountDefalut];
        if (phone.length>0) {
            tf.text = phone;
        }else{
            tf.text = @"李春燕@sy31";
        }
        _acountTF = tf;
    }
    return _acountTF;
}
- (UITextField *)passTF{
    if (!_passTF) {
        UITextField *tf = [[UITextField alloc]init];
        tf.placeholder = @"请输入验证码";
        tf.textColor = TITLECOLOR;
        tf.keyboardType = UIKeyboardTypeNumberPad;
        tf.delegate = self;
        _passTF = tf;
    }
    return _passTF;
}
- (UIButton *)closeButton{
    if (!_closeButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:@"sx_xiaozu_yichu"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
        _closeButton = button;
    }
    return _closeButton;
}

- (UIImageView *)imageView{
    if (!_imageView) {
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.image = [UIImage imageNamed:@"login"];
        _imageView = imageV;
    }
    return _imageView;
}
- (UIImageView *)logoImageView{
    if (!_logoImageView) {
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.image = [UIImage imageNamed:@"logo"];
        _logoImageView = imageV;
    }
    return _logoImageView;
}
- (UILabel *)welcomeLabel{
    if (!_welcomeLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(44.5,375.5,147.5,23);
        label.numberOfLines = 0;
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"WELOME" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Medium" size: 30],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]}];
        label.attributedText = string;
        _welcomeLabel = label;
    }
    return _welcomeLabel;
}
- (UIImageView *)linViewImage{
    if (!_linViewImage) {
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.image = [UIImage imageNamed:@"line_login"];
        _linViewImage = imageV;
    }
    return _linViewImage;
}
- (UIView *)backView{
    if (!_backView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        _backView = view;
    }
    return _backView;
}
- (UIView *)linePassView{
    if (!_linePassView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        _linePassView = view;
    }
    return _linePassView;
}
- (UIView *)lineCountView{
    if (!_lineCountView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        _lineCountView = view;
    }
    return _lineCountView;
}
- (UILabel *)titleLable{
    if (!_titleLable) {
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(750.5,173.5,92.5,22.5);
        label.numberOfLines = 1;
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"忘记密码" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Medium" size: 24],NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}];
        label.attributedText = string;
        _titleLable = label;
    }
    return _titleLable;
}
- (UIImageView *)titleLineImageView{
    if (!_titleLineImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"image_line"];
        _titleLineImageView = imageView;
    }
    return _titleLineImageView;
}
- (UIImageView *)acountImageView{
    if (!_acountImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"forget_phone"];
        _acountImageView = imageView;
    }
    return _acountImageView;
}
- (UIImageView *)passwordImageView{
    if (!_passwordImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"forget_pass"];
        _passwordImageView = imageView;
    }
    return _passwordImageView;
}
//ear_image
//- (UIButton *)acountMoreButton{
//    if (!_acountMoreButton) {
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        [button setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
//        [button addTarget:self action:@selector(acountMoreButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//        _acountMoreButton = button;
//    }
//    return _acountMoreButton;
//}
- (JKCountDownButton *)huoQuYanZhengMaButton{
    if (!_huoQuYanZhengMaButton) {
        JKCountDownButton *button = [JKCountDownButton buttonWithType:UIButtonTypeCustom];
        [button setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        button.layer.cornerRadius = 10;
        button.layer.masksToBounds  =YES;
        button.layer.borderColor = SELECTEDCOLOR.CGColor;
        button.layer.borderWidth = 1;
        [button setTitle:@"发送验证码" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(passMoreButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _huoQuYanZhengMaButton = button;
    }
    return _huoQuYanZhengMaButton;
}

- (UIView *)topLineView{
    if (!_topLineView) {
        UIView *view = [[UIView alloc]init];
        view.frame = CGRectMake(654,321,285,1);
        view.backgroundColor = [UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1.0];
        view.layer.cornerRadius = 0.5;
        _topLineView = view;
    }
    return _topLineView;
}
- (UIView *)bottomLineVIew{
    if (!_bottomLineVIew) {
        UIView *view = [[UIView alloc]init];
        view.frame = CGRectMake(654,321,285,1);
        view.backgroundColor = [UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1.0];
        view.layer.cornerRadius = 0.5;
        _bottomLineVIew = view;
    }
    return _bottomLineVIew;
}
- (UIButton *)loginButton{
    if (!_loginButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:@"下一步" forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColorChange colorwithHexString:@"#CCCCCC"] forSize:CGSizeMake(200, 100)] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:SELECTEDCOLOR forSize:CGSizeMake(200, 100)] forState:UIControlStateSelected];
        button.selected = NO;
        button.userInteractionEnabled = NO;
        [button addTarget:self action:@selector(loginClick:) forControlEvents:UIControlEventTouchUpInside];
        _loginButton = button;
    }
    return _loginButton;
}


- (void)dismissView:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)remanberClick:(MinButtonViewBtn *)sender{
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        sender.topImage.image = [UIImage imageNamed:@"selecSquir"];
    }else{
        sender.topImage.image = [UIImage imageNamed:@"remanber"];
    }
}
- (void)initSubView{
    self.view.frame = CGRectMake(0, 0, APP_WIDTH, APP_HEIGHT);
    self.view.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"HideTabbarNoti" object:nil];
    [self.view addSubview:self.imageView];
    [self.view addSubview:self.logoImageView];
    [self.view addSubview:self.welcomeLabel];
    [self.view addSubview:self.linViewImage];
    [self.view addSubview:self.backView];
    [_backView addSubview:self.titleLable];
    [_backView addSubview:self.titleLineImageView];
    [_backView addSubview:self.topLineView];
    [_backView addSubview:self.bottomLineVIew];
    [_backView addSubview:self.acountTF];
    
    [_backView addSubview:self.acountImageView];
    [_backView addSubview:self.passwordImageView];
//    [_backView addSubview:self.acountMoreButton];
    [_backView addSubview:self.huoQuYanZhengMaButton];
    [_backView addSubview:self.loginButton];
    [_backView addSubview:self.passTF];
    
    [self.view addSubview:self.closeButton];
    
    
}
- (void)initOption{
    [self getPhoneNumber];
}
- (void)layout{
    CGFloat W = 560*fw;
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.width.equalTo(@0).offset(W);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [_logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0).offset(51*fw);
        make.width.equalTo(@0).offset(191*fw);
        make.height.equalTo(@73);
        make.top.equalTo(@0).offset(148*fh);
    }];
    [_welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.logoImageView.mas_left);
        make.top.equalTo(self.logoImageView.mas_bottom).offset(155*fh);
        make.width.equalTo(@150);
        make.height.equalTo(@30);
    }];
    [_linViewImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.welcomeLabel.mas_left);
        make.width.equalTo(@0).offset(50*fw);
        make.height.equalTo(@5);
        make.top.equalTo(self.welcomeLabel.mas_bottom).offset(20);
    }];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageView.mas_right);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backView.mas_centerX);
        make.top.equalTo(@0).offset(174*fh);
        make.width.equalTo(@100);
        make.height.equalTo(@23);
    }];
    [_titleLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backView.mas_centerX);
        make.top.equalTo(self.titleLable.mas_bottom).offset(7*fh);
        make.width.equalTo(@0).offset(45*fw);
        make.height.equalTo(@2);
    }];
    [_topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0).offset(94*fw);
        make.right.equalTo(@0).offset(-85*fw);
        make.height.equalTo(@1);
        make.top.equalTo(self.titleLineImageView.mas_bottom).offset(125*fh);
    }];
    [_bottomLineVIew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0).offset(94*fw);
        make.right.equalTo(@0).offset(-85*fw);
        make.height.equalTo(@1);
        make.top.equalTo(self.topLineView.mas_bottom).offset(70*fh);
    }];
    [_acountImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topLineView.mas_left);
        make.width.equalTo(@18);
        make.height.equalTo(@18);
        make.bottom.equalTo(self.topLineView.mas_bottom).offset(-13);
    }];
    [_passwordImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomLineVIew.mas_left);
        make.width.equalTo(@18);
        make.height.equalTo(@18);
        make.bottom.equalTo(self.bottomLineVIew.mas_bottom).offset(-13);
    }];
//    [_acountMoreButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.topLineView.mas_right);
//        make.centerY.equalTo(self.acountImageView.mas_centerY);
//        make.width.equalTo(@15);
//        make.height.equalTo(@8);
//    }];
    [_huoQuYanZhengMaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bottomLineVIew.mas_right);
        make.centerY.equalTo(self.passwordImageView.mas_centerY);
        make.width.equalTo(@100);
        make.height.equalTo(@30);
    }];
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backView.mas_centerX);
        make.width.equalTo(@0).offset(297*fw);
        make.height.equalTo(@0).offset(52*fh);
        make.top.equalTo(@0).offset(450*fh);
    }];
    [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@0).offset(-50);
        make.top.equalTo(@50);
        make.width.equalTo(@20);
        make.height.equalTo(@20);
    }];
    [_acountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_acountImageView.mas_right).offset(10);
        make.right.equalTo(self->_topLineView.mas_right).offset(-5);
        make.centerY.equalTo(self->_acountImageView.mas_centerY);
        make.height.equalTo(@15);
    }];
    [_passTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_passwordImageView.mas_right).offset(10);
        make.right.equalTo(self->_huoQuYanZhengMaButton.mas_left).offset(-5);
        make.centerY.equalTo(self->_passwordImageView.mas_centerY);
        make.height.equalTo(@15);
    }];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"HideTabbarNoti" object:nil];
}
//- (void)acountMoreButtonClick:(UIButton *)sender{
//
//}

//- (void)passEarButtonClick:(UIButton *)sender{
//
//}
#pragma mark -得到手机号
- (void)getPhoneNumber{
    NSString *acount = [USERDEFAL objectForKey:AcountDefalut];
    [MBProgressHUD mj_showLoading];
    //有用户名，直接用户名获取手机号重置密码
    NSDictionary *parama;
    if (acount.length>0) {
        parama = @{@"account":[USERDEFAL objectForKey:AcountDefalut]};
    }else{
        parama = @{@"account":_acountTF.text};
    }
  //  NSLog(@"参数 %@",parama);
    [SYRequestAPI sy_getPhoneNumber:parama success:^(id responseObject) {
        int code = [[responseObject objectForKey:@"code"] intValue];
        if (code == 10000) {
            self->phoneModel = [GetPhoneModel yy_modelWithJSON:[responseObject objectForKey:@"data"]];
            self->_acountTF.text = self->phoneModel.mobile;
            self->_acountTF.userInteractionEnabled = NO;
        }else{
            [XSInfoView showInfo:@"请输入正确的账号" onView:self];
            [NSObject executeRunloop:^{
                [self.navigationController popViewControllerAnimated:YES];
            } afterDelay:1];
        }
        [MBProgressHUD mj_hideHUD];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        [XSInfoView showInfo:@"请输入正确的账号" onView:self];
        [NSObject executeRunloop:^{
            [self.navigationController popViewControllerAnimated:YES];
        } afterDelay:1];
    }];
}
#pragma mark - 得到验证码的方法
- (void)passMoreButtonClick:(JKCountDownButton *)sender{
    if ([sender.titleLabel.text intValue] == 0) {
        [sender startCountDownWithSecond:59];
    }
    [MBProgressHUD mj_showLoading];
    NSDictionary *parama = @{@"mobile":_acountTF.text,
                             @"verifyType":VerifyType//找回密码 101
                             };
    [SYRequestAPI sy_getYanZhengMaParama:parama success:^(id responseObject) {
        int code = [[responseObject objectForKey:@"code"] intValue];
        if (code == 10000) {
            //发送验证码成功
             [XSInfoView showInfo:@"获取成功" onView:self];
        }else{
            
        }
       
        [MBProgressHUD mj_hideHUD];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
    }];
    
}
#pragma mark - 验证验证码
- (void)validationCode{
    if (_passTF.text.length<=0) {
        [XSInfoView showInfo:@"请输入验证码" onView:self];
    }else{
        [MBProgressHUD mj_showLoading];
        NSDictionary *parama = @{@"mobile":_acountTF.text,
                                 @"verifyType":VerifyType,
                                 @"verifyCode":_passTF.text };
        [SYRequestAPI sy_validationCodeParama:parama success:^(id responseObject) {
            int code = [[responseObject objectForKey:@"code"] intValue];
            if (code == 10000) {
                //验证成功走下一步
                
                self->_resetPassVC = [[ResetPassViewController alloc]init];
                self->_resetPassVC.acountNum = self->_acountTF.text;
                self->_resetPassVC.validKey = [[responseObject objectForKey:@"data"]objectForKey:@"validKey"];
                [self.navigationController pushViewController:self->_resetPassVC animated:YES];
            }else{

            }
            [XSInfoView showInfo:[responseObject objectForKey:@"msg"] onView:self];
            [MBProgressHUD mj_hideHUD];
        } fail:^(NSError *error) {
            [MBProgressHUD mj_hideHUD];
        }];
    }
    
    
//    self->_resetPassVC = [[ResetPassViewController alloc]init];
//    [self.navigationController pushViewController:self->_resetPassVC animated:YES];
}
#pragma mark - 下一步
- (void)loginClick:(UIButton *)sender{
    yanZhengMaString = _passTF.text;
    acountString = _acountTF.text;
    if (yanZhengMaString.length == 0) {
        [XSInfoView showInfo:@"请输入验证码" onView:self];
    }
    if (acountString.length == 0) {
        [XSInfoView showInfo:@"请输入账号" onView:self];
    }
    if (yanZhengMaString.length>0&&acountString.length>0) {
        [self validationCode];
    }
}
//Textfile的代理
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length>0) {
        if (textField == _acountTF) {
            //账号
        }else{
            
        }
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (_acountTF.text.length>0&_passTF.text.length>0) {
        _loginButton.userInteractionEnabled = YES;
        _loginButton.selected = YES;
    }else{
        _loginButton.userInteractionEnabled = NO;
        _loginButton.selected = NO;
    }
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
